package hut.lvming.interceptor;

import hut.lvming.domain.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ResourceInterceptor extends HandlerInterceptorAdapter {
    private List<String> ignoreUrl;
    public ResourceInterceptor(List<String> ignoreUrl){
        this.ignoreUrl=ignoreUrl;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("1111111111111111111111");
        User user= (User) request.getSession().getAttribute("USER_SESSION");
        String uri=request.getRequestURI();
        if(uri.contains("login")) return true;
        if(user!=null){
            if("ADMIN".equals(user.getRole())){
                return true;
            }else if(!"ADMIN".equals(user.getRole())){
                for(String url:ignoreUrl){
                    if(uri.contains(url)){
                        return true;
                    }
                }
            }
        }


        request.setAttribute("msg","您还没有登录，请先登录！");
        request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
        System.out.println("1111111111111111111111");
        return false;
    }
}
